package cn.demoncat.util.websocket.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
import org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean;

import javax.annotation.Resource;

/**
 * WebSocket配置
 * 
 * @author 延晓磊
 *
 * @since 2018年3月29日
 */
@Configuration
public class WebSocketConfig {

	@Resource
	private WebSocketProperties webSocketProperties;

	/**
	 * 服务端点管理器
	 * 
	 * @return
	 * 
	 * @author 延晓磊
	 *
	 * @since 2018年3月29日
	 */
	@Bean
	public ServerEndpointExporter serverEndpointExporter() {
		return new ServerEndpointExporter();
	}

	/**
	 * 服务配置
	 *
	 * @return
	 */
	@Bean
	public ServletServerContainerFactoryBean webSocketContainer() {
		ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
		// 异步发送超时：-1表示无限
		container.setAsyncSendTimeout(-1L);
		// 会话空闲超时：-0表示无限
		container.setMaxSessionIdleTimeout(0L);
		// 缓存区：默认 8 * 1024，超出时会报错 TEXT_FULL_WRITING
		container.setMaxTextMessageBufferSize(webSocketProperties.getTextBuffer());
		container.setMaxBinaryMessageBufferSize(webSocketProperties.getBinaryBuffer());
		return container;
	}
}